package com.app.service.impl;

import com.app.entity.*;
import com.app.mapper.*;
import com.app.service.SubjectService;
import com.app.util.Result;
import com.app.util.RoleUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2021-04-07
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private BindingMapper bindingMapper;
    @Autowired
    private WorksMapper worksMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result findSubject(String newdate) {
        Integer id = RoleUtil.getRoleId(); //学员id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state", '0');
        queryWrapper.eq("sid", id);
        Binding binding = bindingMapper.selectOne(queryWrapper);
        if (binding == null) {
            return Result.fail("您还没有关注教练");
        }
        Integer cid = binding.getCid(); //教练id
        User user = userMapper.selectById(cid);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("nowdate", newdate);
        queryWrapper1.eq("cid", cid);
        List<Subject> list = subjectMapper.selectList(queryWrapper1);
        return Result.success(list).addmessage("coach", user);
    }

    @Override
    public Result findSubjectByTime(String time) {
        Integer id = RoleUtil.getRoleId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("wid");
        queryWrapper.eq("nowdate", time);
        queryWrapper.eq("cid", id);
        List<Subject> list = subjectMapper.selectList(queryWrapper); //查看已经发布的
        List wids = new ArrayList();
        list.forEach(item -> {
            wids.add(item.getWid());
        });
        QueryWrapper queryWrapper1 = new QueryWrapper();
        if (list.size() > 0) {
            queryWrapper1.notIn("id", wids);
        }
        List<Works> works = worksMapper.selectList(queryWrapper1); //查询没有发布的
        return Result.success(works);
    }

    @Override
    public Result addSubjectByCoach(Subject subject) {
        subject.setCid(RoleUtil.getRoleId());
        subjectMapper.insert(subject);
        return Result.success();
    }

    @Override
    public Result findSubjectSatate(String newdate) {
        if (StringUtils.isEmpty(newdate)) {
            newdate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        Integer cid = RoleUtil.getRoleId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cid", cid);
        queryWrapper.eq("nowdate", newdate);
        queryWrapper.orderByAsc("start_time");
        List list = subjectMapper.selectList(queryWrapper);
        return Result.success(list);
    }

    @Override
    public Result orderSubjectByStudent(Integer id) {
        Integer sid = RoleUtil.getRoleId();//学员id
        User user = userMapper.selectById(sid);
        Orders order = new Orders();
        order.setCreateTime(new Date()).setSid(user.getId()).setSubid(id).setTextState(user.getTestStatus());
        orderMapper.insert(order); //插入订单
        subjectMapper.updateById(new Subject().setId(id).setState('1'));
        return Result.success("预约成功");
    }

    @Override
    public Result findUserShenQingByAdmin() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("test_state", '1');
        List list = userMapper.selectList(queryWrapper);
        return Result.success(list);
    }

    @Override
    public Result UserPassShenQingByAdmin(User user) {
        user = userMapper.selectById(user.getId());
        Character testStatus = user.getTestStatus();//当前考试进度
        int i = Integer.parseInt(String.valueOf(testStatus));
        i = i + 1;   //变成科目二
        if (i == 5) {
            i = 0;
        }
        user.setTestStatus(Character.forDigit(i, 10)); //设置考试进度
        user.setTestState('0');//申请状态
        userMapper.updateById(user);//更新
        bindingMapper.delete(new QueryWrapper<Binding>().eq("sid", user.getId())); //删除绑定关系
        return Result.success();
    }
}
